/* 使用方式
 * 寫在frontend.gsp中，會自動呼叫，若GSP檔名與ActionName不同，則得手動加入
 * <script language="javascript" type="text/javascript">viewName();</script>
 *  <script>viewName();</script>
 *  
 * function viewName() {
 *     $(function() {
 *         alert('呼叫成功！');
 *     });
 * }
 */

function serialDetail() {
    $(function() {
        $('a[name="show"]').click(function() {
            //alert($(this).attr("eventId"));
            var eventId = $(this).attr("eventId");
            $('div[name="detail"]').hide();
            $('#detail' + eventId).show('slow');
        });
        $('form').submit(function() {
            //alert('按了');
            if ($('input[type=checkbox]:checked').length == 0) {
                $('#error').show();
                return false;
            } else {
                return true;
            }
        });
    });
}

function dataAgree() {
    $(function() {
        $('form').submit(function() {
            //alert('按了');
            if ($('input[type=checkbox]:checked').length == 0) {
                $('#error').show();
                return false;
            } else {
                return true;
            }
        });
    });
}

function dataFill() {
    $(function() {
        $('a[name="show"]').click(function() {
            //alert($(this).attr("eventId"));
            var eventId = $(this).attr("eventId");
            $('div[name="detail"]').hide();
            $('#detail' + eventId).toggle('slow');
        });
        $.validator.addMethod("min1", function(value, elem, param) {//最少勾選一個
            if ($('input[type=checkbox]:checked').length > 0) {
                $('#error').hide();
                return true;
            } else {
                $('#error').show();
                return false;
            }
        }, "");
        //台灣身份證字號格式檢查程式
        $.validator.addMethod("TWCID", function(value, element, param) {
                var a = new Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'Y', 'W', 'Z', 'I', 'O');
                var b = new Array(1, 9, 8, 7, 6, 5, 4, 3, 2, 1);
                var c = new Array(2);
                var d;
                var e;
                var f;
                var g = 0;
                var h = /^[a-z](1|2)\d{8}$/i;
                if (value.search(h) == -1) {
                        return false;
                } else {
                        d = value.charAt(0).toUpperCase();
                        f = value.charAt(9);
                }
                for (var i = 0; i < 26; i++) {
                        if (d == a[i]) {//a==a
                                e = i + 10; //10
                                c[0] = Math.floor(e / 10); //1
                                c[1] = e - (c[0] * 10); //10-(1*10)
                                break;
                        }
                }
                for (var i = 0; i < b.length; i++) {
                        if (i < 2) {
                                g += c[i] * b[i];
                        } else {
                                g += parseInt(value.charAt(i - 1)) * b[i];
                        }
                }
                if ((g % 10) == f) {
                        return true;
                }
                if ((10 - (g % 10)) != f) {
                        return false;
                }
                return true;
        }, "請輸入有效的身份證字號!");
        $('form').validate({
            errorClass: "error-red",
            rules: {
                CID: "TWCID",
                email1: {email: true},
                email2: {email: true},
//                reg: {required: true,minlength: 1}
                birth: {dateISO: true},
                birth_year: {required: true},
                birth_month: {required: true},
                birth_day: {required: true},
                reg: "min1"
            },
            messages: {
//                reg: {min1:"ONE"}
                birth: {dateISO: "日期格式：1964/2/9"}
            }
        });
    });
}

function dataCheck() {
    $(function() {
        $.validator.addMethod("min2", function(value, elem, param) {
            if ($('input[type=checkbox]:checked').length > 1) {
                $('#error').hide();
                return true;
            } else {
                $('#error').show();
                return false;
            }
        }, "");
        $('form').validate({
            errorClass: "error-red",
            rules: {
                recheck: "min2"
            },
            messages: {
            }
        });
    });
}

function regQuery() {
    $(function() {
        $('form').validate({
            errorClass: "error-red",
            rules: {
                birth: {dateISO: true}
            },
            messages: {
                birth: {dateISO: "格式錯誤"}
            }
        });
    });
}

function regCancelConfirm() {
    $(function() {
        $(".detail").click(function() {
        $("#thedialog").attr('src', $(this).attr("href"));
            $("#somediv").dialog({
                width: 740,
                height: 570,
                modal: true,
                close: function() {
                    $("#thedialog").attr('src', "about:blank");
                }
            });
        return false;
        });
    });
}
    
function regQueryResult() {
    $(function() {
        $(".detail").click(function() {
            $("#thedialog").attr('src', $(this).attr("href"));
            $("#somediv").dialog({
                width: 740,
                height: 570,
                modal: true,
                close: function() {
                    $("#thedialog").attr('src', "about:blank");
                }
            });
            return false;
        });
        $('a[name="show"]').click(function() {
            //alert($(this).attr("eventId"));
            var eventId = $(this).attr("eventId");
            $('div[name="detail"]').hide();
            $('#detail' + eventId).show('slow');
        });
        $.validator.addMethod("min1", function(value, elem, param) {//最少勾選一個
            if ($('input[type=checkbox]:checked').length > 0) {
                $('#error').hide();
                return true;
            } else {
                $('#error').show();
                return false;
            }
        }, "");
        $('form').validate({
            errorClass: "error-red",
            rules: {
                cancel: "min1"
            },
            messages: {
            }
        });
    });
}

function regCancel() {
    $(function() {
        $('form').validate({
            errorClass: "error-red",
            rules: {
                cancelCode: {minlength: 36, maxlength: 36}
            },
            messages: {
                cancelCode: {minlength: "取消碼錯誤", maxlength: "取消碼錯誤"}
            }
        });
    });
}


